package com.xbongbong.formula.qlexpress.operator.date;

import com.ql.util.express.Operator;
import com.xbongbong.formula.constant.Constant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.PaasDateUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BusinessRuleErrorCodeEnum;

import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/8/9 9:51
 * @since v1.0
 */
public class OperatorDate extends Operator {


    public OperatorDate(String name){
        this.name = name;
    }

    public OperatorDate(String aliasName, String aName, String errorInfo){
        this.aliasName = aliasName;
        this.name = aName;
        this.errorInfo = errorInfo;
    }

    /**
     * 时间戳转换为日期对象
     * 语法：DATE(year,month,day,[hour,minute,second])
     * 说明：将年月日时分秒转换为日期
     * 例如：手动固定某一个时间 。
     * @param list
     * @return 时间戳
     */
    @Override
    public Object executeInner(Object[] list) throws XbbException {

        if (Objects.isNull(list)) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261007);
        }
        if (list.length == Constant.SCALE) {
            String date = list[0].toString() + "-" + list[1].toString() + "-" + list[2].toString();
            return DateTimeUtil.getInt(date, DateUtil.SDFDate);
        } else if (list.length == Constant.FOUR) {
            String date = list[0].toString() + "-" + list[1].toString() + "-" + list[2].toString();
            date = date +" " + list[3].toString();
            return DateTimeUtil.getInt(date, "yyyy-MM-dd HH");
        } else if (list.length == Constant.FIVE) {
            String date = list[0].toString() + "-" + list[1].toString() + "-" + list[2].toString();
            date = date +" " + list[3].toString() + ":" + list[4].toString() ;
            return DateTimeUtil.getInt(date, DateUtil.SDFYMDHM);
        } else if (list.length == Constant.SIX) {
            String date = list[0].toString() + "-" + list[1].toString() + "-" + list[2].toString();
            date = date +" " + list[3].toString() + ":" + list[4].toString() + ":" + list[5].toString() ;
            return DateTimeUtil.getInt(date, DateUtil.SDF);
        } else {
            if (Objects.isNull(list[0])) {
                return null;
            }
            return PaasDateUtil.getInt(list[0]);
        }
    }

}
